import pymysql
import time
import random

db_config = {
        'host':'122.114.195.118',
        'port':3306,
        'user': 'admin',
        'password': 'Root110qwe',
        'db': 'crm',
        'charset': 'utf8',
    }

def connect_mysql_sql(cur,conn,sql_2,moshi='select'): #连接MySQL
    cur.execute(sql_2)
    conn.commit()
    if moshi == 'select':
        tuple_rcs = cur.fetchall()
        return tuple_rcs
    elif moshi == 'select_1':
        tuple_rcs = cur.fetchone()
        return tuple_rcs
day = 0
li = []
sql_1 = "SELECT id,zong_ip,current_work_num FROM tb_work WHERE is_delete=0 and status=1"
conn = pymysql.connect(**db_config)
cur = conn.cursor()
id_li = connect_mysql_sql(cur,conn,sql_1)
for i in id_li:
    li.append(i[0])
while True:
    id = random.choice(li)
    sql_2 = "SELECT zong_ip,current_work_num FROM tb_work WHERE id={};".format(id)
    note = connect_mysql_sql(cur,conn,sql_2,'select_1')
    if note[0]>note[-1]:
        current_work_num = note[-1] + 1
        sql_updata = 'update tb_work set current_work_num={} where id={};'.format(current_work_num, id)
        connect_mysql_sql(cur, conn, sql_updata, 'update')
    print(f'进度修改完成{id}')
    time.sleep(30)

# while True:                                                                                                                                                                                #     day += 1
#     for i in range(24):
#         conn = pymysql.connect(**db_config)
#         cur = conn.cursor()
#         try:
#             sql = "SELECT id,zong_ip,current_work_num FROM tb_work WHERE is_delete=0 and status=1"
#             tuple_data = connect_mysql_sql(cur,conn,sql)
#             for i in tuple_data:
#                 if i[1]>i[-1]:
#                     current_work = i[-1] + 1
#                     sql_updata = 'update tb_work set current_work_num={} where id={};'.format(current_work,i[0])
#                     connect_mysql_sql(cur, conn, sql_updata, moshi='update')
#         except Exception as e:
#             print(e)
#         finally:
#             cur.close()
#             conn.close()
#         time.sleep(60*60)
#     print('第{}天'.format(day))
#     time.sleep(60*60*24)